import _ from 'lodash';
import moment from 'moment';
import { message } from 'antd';

// 判断该用户是否有权限
export const havePermissions = (buttonType: string, buttonPermissions: any) => {
  const hasSave =
    buttonPermissions?.filter((item: any) => item.menuCode === buttonType)?.length > 0;
  if (!hasSave) {
    message.info('请联系管理员获取相关权限');
    return false;
  } else {
    return true;
  }
};

// 数据保留小数位
export const ToFixed = (data: any, persion = 3) => {
  if (!data) return data;
  const dot = String(data).indexOf('.');
  let num = Number(data);
  if (dot !== -1) {
    const dotCnt = String(data).substring(dot + 1, data.length);
    if (dotCnt.length > persion) {
      num = Number(Number(data).toFixed(persion));
    } else {
      num = Number(data);
    }
  }
  return num;
};

export const TIME_ENUM = (point: 96 | 24, format: string = 'HHmm') => {
  const startTimeNum = format === 'HHmm' ? '0000' : '00:00';
  const startPointNum = format === 'HHmm' ? '2400' : '24:00';
  const timeArr = new Array(point);
  const time = moment().startOf('date');
  const pointArray: string[] = [];
  if (point === 96) {
    _.forEach(timeArr, () => {
      time.add(15, 'minutes');
      const timeData = `${
        time.format(format) === startTimeNum ? startPointNum : time.format(format)
      }`;
      pointArray.push(timeData);
    });
  } else {
    _.forEach(timeArr, () => {
      time.add(60, 'minutes');
      const timeData = `${
        time.format(format) === startTimeNum ? startPointNum : time.format(format)
      }`;
      pointArray.push(timeData);
    });
  }
  return pointArray;
};

//中文对比
export function compareFunction(data: any) {
  const result: any = [];
  const result_data = [...data];
  result_data.map((item) => {
    const dataBodyList = compareFunctionArr([...item.dataBodyList]);
    result.push({
      ...item,
      data: dataBodyList,
    });
  });
  return result;
}

export function compareFunctionArr(data: any) {
  return data.sort((a: any, b: any) => a.stationName.localeCompare(b.stationName));
}
// 格式化导出数据
export const formatJson = (filterVal: any[], jsonData: any[]) => {
  return jsonData.map((v) => {
    return filterVal.map((j) => {
      if (v) {
        return v[j];
      } else {
        return '-';
      }
    });
  });
};
//时间格式处理
export const dealDateTime = (date: any, time: any) => {
  return date && time
    ? moment(`${date} ${time}`).format('HHmm') === '0000'
      ? moment(`${date} ${time}`).subtract(1, 'day').format('YYYYMMDD 2400')
      : moment(`${date} ${time}`).format('YYYYMMDD HHmm')
    : '-';
};
//数据处理
export const dealDataSource = (data: any) => {
  return data.map((t: any) => ({
    ...t,
    startDate: dealDateTime(t.startDate, t.startTime),
    endDate: dealDateTime(t.endDate, t.endTime),
    tendingDateTime: t.tendingDateTime ? moment(t.tendingDateTime).format('YYYYMMDD HHmmss') : '-',
    tendingEvent: t.tendingEvent || '-',
    planState:
      t.planState === 0
        ? '未开始'
        : t.planState === 1
        ? '已取消'
        : t.planState === 2
        ? '已完成'
        : '',
  }));
};
//时点转换
export const pointConvert = (num = 0) => {
  const _num =
    num % 4 == 0 ? num * 25 : num / 4 > 0 ? num * 15 + Math.floor(num / 4) * 40 : num * 15;
  return _num < 100 ? '00' + _num : _num < 1000 ? '0' + _num : _num.toString();
};
export const enumerateDaysBetweenDates = (startDate: any, endDate: any) => {
  const daysList = [];
  const start = moment(startDate);
  const end = moment(endDate);
  const day = end.diff(start, 'day');
  daysList.push(start.format('YYYY-MM-DD'));
  for (let i = 1; i <= day; i++) {
    daysList.push(start.add(1, 'day').format('YYYY-MM-DD'));
  }
  return daysList;
};
